<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Road Condition Detector</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            line-height: 1.6;
            color: #333;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f4f4f4;
        }
        h1 {
            color: #2c3e50;
            text-align: center;
        }
        .upload-container {
            background-color: #fff;
            border-radius: 8px;
            padding: 20px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        #imageInput {
            display: none;
        }
        .custom-file-upload {
            display: inline-block;
            padding: 10px 20px;
            cursor: pointer;
            background-color: #3498db;
            color: white;
            border-radius: 5px;
            transition: background-color 0.3s;
        }
        .custom-file-upload:hover {
            background-color: #2980b9;
        }
        #uploadButton {
            display: block;
            width: 100%;
            padding: 10px;
            margin-top: 20px;
            background-color: #2ecc71;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            transition: background-color 0.3s;
        }
        #uploadButton:hover {
            background-color: #27ae60;
        }
        #result {
            margin-top: 20px;
            background-color: #fff;
            border-radius: 8px;
            padding: 20px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        #resultImage {
            max-width: 100%;
            border-radius: 8px;
            margin-top: 20px;
        }
    </style>
</head>
<body>
    <h1>Road Condition Detector</h1>
    <div class="upload-container">
        <label for="imageInput" class="custom-file-upload">
            Choose Image
        </label>
        <input type="file" id="imageInput" accept="image/*">
        <button id="uploadButton">Detect Road Conditions</button>
    </div>
    <div id="result"></div>

    <script>
        document.getElementById('uploadButton').addEventListener('click', async () => {
            const fileInput = document.getElementById('imageInput');
            const file = fileInput.files[0];
            if (!file) {
                alert('Please select an image file');
                return;
            }

            const reader = new FileReader();
            reader.onload = async (e) => {
                const base64Image = e.target.result.split(',')[1];
                
                try {
                    const response = await fetch('http://localhost:8000/detect', {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/json',
                        },
                        body: JSON.stringify({ base_file: base64Image }),
                    });

                    if (!response.ok) {
                        throw new Error(`HTTP error! status: ${response.status}`);
                    }

                    const data = await response.json();
                    const resultDiv = document.getElementById('result');
                    resultDiv.innerHTML = `
                        <h2>Detection Results:</h2>
                        <p>${data.detected_classes}</p>
                        <img id="resultImage" src="data:image/png;base64,${data.image_with_boxes}" alt="Detected objects">
                    `;
                } catch (error) {
                    console.error('Error:', error);
                    alert('An error occurred while processing the image.');
                }
            };
            reader.readAsDataURL(file);
        });
    </script>
</body>
</html>